<?php

class SelectControl extends ValueControl {

	private $width = '';
	public function WithWidth($value){ $this->width = $value; return $this; }

	private $rows = 1;
	public function WithRows($value){ $this->rows = $value; return $this; }

	private $allow_null = false;
	public function WithAllowNull($value){ $this->allow_null = $value; return $this; }

	private $is_multiple = false;
	public function WithIsMultiple($value){ $this->is_multiple = $value; return $this; }

	private $use_check_boxes = false;
	public function WithCheckBoxes($value){ $this->use_check_boxes = $value; return $this; }

	private $style = '';
	public function WithStyle($value){ $this->style = $value; return $this; }

	private $display = null;
	public function WithDisplay($value){ $this->display = $value; return $this; }

	private $on_change = '';
	public function WithOnChange($value){ $this->on_change = $value; return $this; }

	private $list_values = array();
	private $list_captions = array();
	public function Add($value,$caption=null){
		$this->list_values[] = strval( new Url( $value ) );
		if (is_null($caption)){
			$html = new Html($value);
			$caption = $html->Export();
		}
		$this->list_captions[] = $caption;
	}

	public function AddDBItem(DBItem $dbitem){
		return $this->Add( $dbitem->id , $dbitem->GetCode() . ' &rarr; ' . $dbitem->GetTitle() );
	}
	public function AddDBItemCodeOnly(DBItem $dbitem){
		return $this->Add( $dbitem->id , $dbitem->GetCode());
	}
	public function AddDBItemTitleOnly(DBItem $dbitem){
		return $this->Add( $dbitem->id , $dbitem->GetTitle() );
	}

	public function WithOption($value,$caption=null){
		$this->Add($value,$caption);
		return $this;
	}
	public function WithDBList($dblist){
		foreach ($dblist as $x) $this->AddDBItem($x);
		return $this;
	}
	public function WithDBListCodeOnly($dblist){
		foreach ($dblist as $x) $this->AddDBItemCodeOnly($x);
		return $this;
	}
	public function WithDBListTitleOnly($dblist){
		foreach ($dblist as $x) $this->AddDBItemTitleOnly($x);
		return $this;
	}

	private function IsSelected($value){
		if ($this->is_multiple){
			if (!is_null($this->value)){
				foreach ($this->value as $x) {
					if ((is_null($x) || $x == '') && (is_null($value) || $value==''))
						return true;
					$url = new Url($x);
					if ($value == $url->Export())
						return true;
				}
			}
			return false;
		}
		else {
			$html = new Html($this->value);
			return $value == $html->Export();
		}
	}

	public function Render(){
		if ($this->mode == UIMode::Edit){
			if ($this->use_check_boxes) {
				echo '<div class="formPane" style="'.(empty($this->display)?'':'display:'.$this->display.';').'height:'.($this->rows*23).'px;width:'.(empty($this->width)?'auto':$this->width).';overflow:auto;white-space:nowrap;'.$this->style.'">';

				if ( $this->allow_null )
					echo '<div><input type="'.($this->is_multiple?'checkbox':'radio').'" name="'.$this->name.($this->is_multiple?'[]':'').'" id="'.$this->name.'_null" value="'.$this->list_values[$i].'"'.($this->IsSelected(null)?' checked="checked"':'').' onclick="'.$this->on_change.'" /><label for="'.$this->name.'_null">&nbsp;</label></div>';

				for ($i = 0; $i < count($this->list_values); $i++)
					echo '<div><input type="'.($this->is_multiple?'checkbox':'radio').'" name="'.$this->name.($this->is_multiple?'[]':'').'" id="'.$this->name.$i.'" value="'.$this->list_values[$i].'"'.($this->IsSelected($this->list_values[$i])?' checked="checked"':'').' onclick="'.$this->on_change.'" /><label for="'.$this->name.$i.'">'.$this->list_captions[$i].'</label></div>';

				echo '</div>';
			}
			else {
				echo '<select id="'.$this->name.($this->is_multiple?'[]':'').'"';
				echo ' name="'.$this->name.'"';
				echo ' class="formPane"';
 				echo ' onchange="'.$this->on_change.'"';
 				echo ' style="'.(empty($this->display)?'':'display:'.$this->display.';').'width:'.(empty($this->width)?'auto':$this->width).';'.$this->style.'"';
				if ($this->is_multiple) echo ' multiple="multiple"';
				echo ' size="'.$this->rows.'">';

				if ( $this->allow_null )
					echo '<option value=""'.($this->IsSelected(null)?' selected="selected"':'').'>&nbsp;</option>';

				for ($i = 0; $i < count($this->list_values); $i++)
					echo '<option value="'.$this->list_values[$i].'"'.($this->IsSelected($this->list_values[$i])?' selected="selected"':'').'>'.$this->list_captions[$i].'</option>';

				echo '</select>';
			}
		}
		else {
			$j = 0;
			for ($i = 0; $i < count($this->list_values); $i++) {
				if ($this->IsSelected($this->list_values[$i])) {
					if ($j++>0) echo ', ';
					echo $this->list_captions[$i];
					break;
				}
			}
		}
	}

}


?>
